package com.flanz.apache.velocity.uimocker;

import java.io.File;
import java.io.FileNotFoundException;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * MimeTable.
 * 
 * @author cf
 * 
 */
public class MimeTable {
	private static Logger LOG = LoggerFactory.getLogger(MimeTable.class);
	private Configuration instanceMAP = null;

	public MimeTable(final String fileName) throws FileNotFoundException {
		instanceMAP = loadMap(fileName);
	}

	private static String getMimeType(final Configuration instanceMAP2,
			final File fromFileName) {
		String result = "application/octet-stream";
		String name = fromFileName.getName();
		int lastIndexOf = name.lastIndexOf('.');
		if (lastIndexOf != -1) {
			String match = name.substring(lastIndexOf + 1);
			Object object = instanceMAP2.getProperty(match);
			if (object != null) {
				result = object.toString();
			}

		}

		return result;
	}

	public String getMimeType(final File fromFileName) {
		return getMimeType(instanceMAP, fromFileName);
	}

	private static Configuration loadMap(final String resource) {
		Configuration properties = null;
		try {
			properties = new PropertiesConfiguration(resource);
		} catch (ConfigurationException e) {
			LOG.error("ConfigurationException", e);
		}
		return properties;
	}

}
